<?php
namespace PFT;
/**
 * Created by PhpStorm.
 * User: Guangpeng Chen
 * Date: 15-1-15
 * Time: 下午3:25
 */
class Db {
    private static $host       = '127.0.0.1';
    private static $username   = 'admin';
    private static $pwd        = 'adm*753951';
    private static $dbname     = 'myuu';
    private static $port       = 3306;
    private static $con        = null;

    private function __construct(){}

    public static function Conf(Array $conf)
    {
        self::$host     = $conf['host'];
        self::$username = $conf['username'];
        self::$pwd      = $conf['pwd'];
        self::$dbname   = $conf['dbname'];
    }
    public static function Connect()
    {
        if (is_null(self::$con)) {
            $dsn = "mysql:dbname=" . self::$dbname.";host=".self::$host.";port=" . self::$port;
            $pdo = new \PDO($dsn, self::$username, self::$pwd);
            $pdo->exec('SET NAMES "UTF8"');
            self::$con = $pdo;
            return self::$con;
        } else {
            return self::$con;
        }
    }

    public static function fetchAll($sql, Array $param=array())
    {
        $stmt = self::Connect()->prepare($sql);
        $stmt->execute($param);
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }

    public static function fetch($sql, Array $param=array())
    {
        $stmt = self::Connect()->prepare($sql);
        $stmt->execute($param);
        return $stmt->fetch(\PDO::FETCH_ASSOC);
    }
	
	public static function lastInsertId(){
		return self::Connect()->lastInsertId();
	}
	
	
	// 执行增删改
	public static function execute($sql, Array $param=array()){
		$stmt = self::Connect()->prepare($sql);
		return $stmt->execute($param);
	}

	
	
	
	
}